package cn.uestc.labhms.modules.hms.service.impl;

import cn.uestc.labhms.modules.hms.dao.HmsChiEngDao;
import cn.uestc.labhms.modules.hms.dao.XlsxDao;
import cn.uestc.labhms.modules.hms.listener.NoModelDataListener;
import cn.uestc.labhms.modules.hms.service.ExcelService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class ExcelServiceImpl implements ExcelService {
    @Autowired
    private HmsChiEngDao hmsChiEngDao;
    @Autowired
    private XlsxDao xlsxDao;

    @Override
    public void uploadAndImport(MultipartFile file) throws IOException {
        // 导入数据库
        ExcelReader build = EasyExcel.read(file.getInputStream()).build();
        List<ReadSheet> readSheets = build.excelExecutor().sheetList();
        List<ReadSheet> myReadSheets = new LinkedList<>();
        StopWatch stopWatch = new StopWatch("xlsx读取时间");
        stopWatch.start();
        for (ReadSheet readSheet : readSheets) {
//            EasyExcel.read(file, new NoModelDataListener(hmsChiEngDao, xlsxDao,entry.getValue())).sheet(entry.getKey()).doRead();
            // 构建表单链
            myReadSheets.add(EasyExcel.readSheet(readSheet.getSheetNo())
                    .registerReadListener(new NoModelDataListener(hmsChiEngDao, xlsxDao, readSheet.getSheetName()))
                    .build());
        }
        // 读取全部表单
        build.read(myReadSheets);
        // 清楚临时文件
        build.finish();
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
    }
}
